package com.facebook.common.executors;

import android.os.Handler;

import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nullable;

public class HandlerExecutorServiceImpl extends AbstractExecutorService implements HandlerExecutorService {
    final Handler mHandler;

    public HandlerExecutorServiceImpl(Handler handler) {
        this.mHandler = handler;
    }

    private static ScheduledFutureImpl a(Runnable arg1, Object arg2) {
        return new ScheduledFutureImpl(arg1, arg2);
    }

    private static ScheduledFutureImpl a(Callable callable) {
        return new ScheduledFutureImpl(callable);
    }

    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
        throw new UnsupportedOperationException();
    }

    private <T> ScheduledFuture<T> b(Runnable task, @Nullable T result) {
        if (task == null) {
            throw new NullPointerException();
        }
        ScheduledFutureImpl<T> future = HandlerExecutorServiceImpl.a(task, result);
        this.execute(future);
        return future;
    }

    public void execute(Runnable runnable) {
        this.mHandler.post(runnable);
    }

    public boolean isShutdown() {
        return false;
    }

    public boolean isTerminated() {
        return false;
    }

    protected RunnableFuture newTaskFor(Runnable runnable, Object arg3) {
        return HandlerExecutorServiceImpl.a(runnable, arg3);
    }

    protected RunnableFuture newTaskFor(Callable arg2) {
        return HandlerExecutorServiceImpl.a(arg2);
    }

    public ScheduledFuture schedule(Runnable arg5, long arg6, TimeUnit arg8) {
        ScheduledFutureImpl v0 = HandlerExecutorServiceImpl.a(arg5, null);
        this.mHandler.postDelayed(((Runnable) v0), arg8.toMillis(arg6));
        return ((ScheduledFuture) v0);
    }

    public ScheduledFuture schedule(Callable callable, long arg6, TimeUnit arg8) {
        ScheduledFutureImpl v0 = HandlerExecutorServiceImpl.a(callable);
        this.mHandler.postDelayed(((Runnable) v0), arg8.toMillis(arg6));
        return ((ScheduledFuture) v0);
    }

    public ScheduledFuture scheduleAtFixedRate(Runnable arg2, long arg3, long arg5, TimeUnit arg7) {
        throw new UnsupportedOperationException();
    }

    public ScheduledFuture scheduleWithFixedDelay(Runnable arg2, long arg3, long arg5, TimeUnit arg7) {
        throw new UnsupportedOperationException();
    }

    public void shutdown() {
        throw new UnsupportedOperationException();
    }

    public List shutdownNow() {
        throw new UnsupportedOperationException();
    }

    public Future submit(Runnable arg2) {
        return this.b(arg2, null);
    }

    public Future submit(Runnable arg2, Object arg3) {
        return this.b(arg2, arg3);
    }

    public<T> Future<T> submit(Callable<T> callbable) {
        if (callbable == null) {
            throw new NullPointerException();
        }
        ScheduledFutureImpl v0 = HandlerExecutorServiceImpl.a(callbable);
        this.execute(((Runnable) v0));
        return ((Future) v0);
    }
}

